home *** CD-ROM | disk | FTP | other *** search
- page 132,63,1,1
- opt rc
- title 'SCI Handling'
-
- ;***************************************************************
- ;* SCI.ASM -- Asyncronous Serial Interface Routines *
- ;* *
- ;* Provides interrupt based input/output routines for the *
- ;* serial line. *
- ;* *
- ;* This module reserves registers as follows: *
- ;* r3 - input/output sample pointer *
- ;* *
- ;* Copyright (C) 1992 by Alef Null. All rights reserved. *
- ;* Author(s): Jarkko Vuori, OH2LNS *
- ;* Modification(s): *
- ;* 09-Aug-1992: corrected xmit interrupt handler bug *
- ;***************************************************************
-
- section SCI
- xdef sci_ini,putc,getc
- xdef sci_rec,sci_err,sci_xmt
- xref scix,scir
-
- nolist
- include 'ioequlc.asm'
- include 'macros.asm'
- list
-
- org p:
-
- ; SCI parameters
- baud equ 9600 ; SCI baud rate
- xtal equ 20763000 ; XTAL frq (in MHz)
-
- ; SCI flags
- rempty equ 0
- rfull equ 1
- xempty equ 2
- xfull equ 3
-
-
- ;****************************
- ;* SCI initialization *
- ;****************************
- sci_ini
- ; initialize SCI
- movep #$0b02,x:m_scr ; 8,n,1
- movep #xtal/(2*2*16*baud),x:m_sccr
-
- ; initialize queue handling
- move #outbuf,a1
- move a1,x:xhead
- move a1,x:xtail
- move #inbuf,a1
- move a1,x:rhead
- move a1,x:rtail
-
- move #buflen-1,m3
-
- ; and other data structures
- movi (1<<rempty)|(1<<xempty),y:flags ; buffer empty at first
-
- rts
-
-
- ;****************************
- ;* Put character to queue *
- ;****************************
- ; byte in x0
- ; returns Z if buffer full
- ; NZ otherwise
- putc ori #$03,mr ; disable interrupts
- move x:xtail,a
- move x:xhead,x1
- move x1,r3
-
- ; check for idling xmitter
- bset #m_tie,x:m_scr
- jcc <kickput
-
- ; xmitter was running, check if there are free space left
- bclr #xempty,y:flags
- jcs <putc1
- cmp x1,a
- jne <putc1
-
- ; buffer full state reached (ignore given data)
- bset #xfull,y:flags
- jmp <putce
-
- ; there was free space left, write character to the buffer
- putc1 move x0,p:(r3)+
- move r3,x:xhead
- jmp <putce
-
- ; xmitter was stopped, kick it up
- kickput movep x0,x:m_stxl
-
- putce andi #$fc,mr
- rts
-
-
- ;****************************
- ;* Get character from queue *
- ;****************************
- ; byte in x0
- ; returns C if no data available
- ; NC if data available
- getc ori #$03,mr ; disable interrupts
- nop
- nop
- nop
- nop
- move x:rtail,r3
-
- ; check if there are data available
- btst #rempty,y:flags
- jcs <getce
-
- ; yes, take it from the queue
- bclr #rfull,y:flags
- move p:(r3)+,x0
- move r3,x:rtail
-
- ; check if buffer gets empty
- move x:rhead,a
- move r3,x1
- cmp x1,a
- andi #$fe,ccr
- jne <getce
-
- ; yes, set empty flag
- bset #rempty,y:flags
-
- getce andi #$fc,mr
- rts
-
-
- ;****************************
- ;* SCI xmit interrupt *
- ;****************************
- sci_xmt enter scix
-
- move x:xtail,r3
- bclr #xfull,y:flags
- jcs <scix1
-
- ; check if buffer empty
- move x:xhead,x0
- move r3,a
- cmp x0,a
- jeq <scixep
-
- ; no, put data out
- scix1 movep p:(r3)+,x:m_stxl
- move r3,x:xtail
- jmp <scixe
-
- ; yes, shut down xmitter
- scixep bclr #m_tie,x:m_scr
- bset #xempty,y:flags
-
- scixe leave scix
-
-
- ;****************************
- ;* SCI receive interrupt *
- ;****************************
- sci_rec enter scir
-
- movep x:m_srxl,a
-
- ; first check that there are room left in the buffer
- btst #rfull,y:flags
- jcs <scire
-
- ; yes, the put data there
- move x:rhead,r3
- bclr #rempty,y:flags
- move a1,p:(r3)+
- move r3,x:rhead
-
- ; check buffer full condition
- move r3,a
- move x:rtail,x1
- cmp x1,a
- jne <scire
- bset #rfull,y:flags
-
- scire leave scir
-
-
- ;****************************
- ;* SCI receive with errors *
- ;* interrupt *
- ;****************************
- sci_err movep x:m_ssr,y:tmp ; clear SCI interrupts
- movep x:m_srxl,y:tmp
-
- rti
-
-
- org x:
-
- rhead ds 1
- rtail ds 1
- xhead ds 1
- xtail ds 1
-
-
- org y:
-
- flags ds 1
- tmp ds 1
-
- endsec
-
-
- section SCIbuf
-
- xdef buflen
- xdef inbuf,outbuf
-
- buflen equ 512 ; SCI input/output buffer lenght
-
- inbuf ds buflen
- outbuf ds buflen
-
- endsec
-
- end
-